/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReportGenerator.java
 *
 * Created on Nov 28, 2011, 11:24:47 PM
 */


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author asus
 */
public class ReportGenerator extends javax.swing.JFrame {

    /** Creates new form ReportGenerator */
    public ReportGenerator() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        salaryReportTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        exia = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                loadSalaryReportData(evt);
            }
        });

        salaryReportTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Employee ID", "First Name", "Last Name", "Base Salary", "SSS", "Pag-ibig Loan", "Pag-ibig Contract", "Total Salary", "Date Received"
            }
        ));
        jScrollPane1.setViewportView(salaryReportTable);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Salary Report");

        jButton1.setActionCommand("Back");
        jButton1.setLabel("Back");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("This salary was given on the month of :");

        exia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }));
        exia.setSelectedIndex(2);
        exia.setSelectedItem(1);
        exia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exiaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 942, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 571, Short.MAX_VALUE)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(exia, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(12, 12, 12)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(exia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        new AdminScreen2().setVisible(true);
        this.setVisible(false);
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void loadSalaryReportData(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_loadSalaryReportData
        Connection connection = Connector.get();
        
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * from salaryarchives");
            //System.out.println("hello");
            
            
            while (rs.next()) {
                //System.out.print("BS");
                String employeeID = rs.getString("EmployeeID");
                //System.out.print(employeeID);
                String firstName = rs.getString("FirstName");
                String lastName = rs.getString("LastName");
                String startingSalary = rs.getString("BaseSalary");
                String SSS = rs.getString("SSS");
                String pagIbigLoan = rs.getString("Pag-IBIGLoan");
                String pagIbigContract = rs.getString("Pag-IBIGContract");
                String totalSalary = rs.getString("TotalSalary");
                String dateGiven = rs.getString("DateReceived");
                
                DefaultTableModel tblModel = (DefaultTableModel)salaryReportTable.getModel();
                Object[] x = {employeeID, firstName, lastName, startingSalary, SSS, pagIbigLoan, pagIbigContract, totalSalary, dateGiven};
                
                if(dateGiven.split(" ")[0].substring(0, 3).equalsIgnoreCase(exia.getSelectedItem().toString()))
                    tblModel.addRow(x);
            }
        }
        catch (SQLException ex) {
            System.err.println("Error loading salary info. Program will now exit.");
            System.exit(1);
        }
        finally {
            try {
                connection.close();
            }
            catch (Exception ex) {
                
            }
        }
    }//GEN-LAST:event_loadSalaryReportData

    
    private void exiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exiaActionPerformed
        Connection connection = Connector.get();
        DefaultTableModel tblModel = (DefaultTableModel)salaryReportTable.getModel();
                
        while(tblModel.getRowCount() != 0)
        {
            tblModel.removeRow(0);
        }
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * from salaryarchives");
            
            while (rs.next()) {
                String employeeID = rs.getString("EmployeeID");
                String firstName = rs.getString("FirstName");
                String lastName = rs.getString("LastName");
                String startingSalary = rs.getString("BaseSalary");
                String SSS = rs.getString("SSS");
                String pagIbigLoan = rs.getString("Pag-IBIGLoan");
                String pagIbigContract = rs.getString("Pag-IBIGContract");
                String totalSalary = rs.getString("TotalSalary");
                String dateGiven = rs.getString("DateReceived");
                
                
                
                Object[] x = {employeeID, firstName, lastName, startingSalary, SSS, pagIbigLoan, pagIbigContract, totalSalary, dateGiven};
               
                if(dateGiven.split(" ")[0].substring(0, 3).equalsIgnoreCase(exia.getSelectedItem().toString()))
                tblModel.addRow(x);
            }
        }
        catch (SQLException ex) {
            System.err.println("Error loading salary info. Program will now exit.");
            ex.printStackTrace();
        }
        finally {
            try {
                connection.close();
            }
            catch (Exception ex) {
                
            }
        }

    }//GEN-LAST:event_exiaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ReportGenerator().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox exia;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable salaryReportTable;
    // End of variables declaration//GEN-END:variables
}
